/*
 * Copyright (c) 2005, the JUNG Project and the Regents of the University of
 * California All rights reserved.
 *
 * This software is open-source under the BSD license; see either "license.txt"
 * or http://jung.sourceforge.net/license.txt for a description.
 *
 * Created on Aug 18, 2005
 */

package at.tugraz.kmi.css.commons;

import javax.annotation.Nonnull;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;

/**
 * 
 * Default implementation of a change event sender. No serializaton!.
 * 
 * @author Georg Oettl
 * 
 */
public class ChangeEventSupport {

	Object							eventSource;

	/**
	 * holds the registered listeners
	 */
	protected EventListenerList		listenerList	= new EventListenerList();

	/**
	 * Only one <code>ChangeEvent</code> is needed instance since the event's
	 * only state is the source property. The source of events generated is
	 * always "this".
	 */
	protected transient ChangeEvent	changeEvent;

	public ChangeEventSupport(Object eventSource) {
		this.eventSource = eventSource;
	}

	public void addChangeListener(@Nonnull ChangeListener l) {
		listenerList.add(ChangeListener.class, l);
	}

	public void removeChangeListener(@Nonnull ChangeListener l) {
		listenerList.remove(ChangeListener.class, l);
	}

	@Nonnull
	public ChangeListener[] getChangeListeners() {
		return listenerList.getListeners(ChangeListener.class);
	}

	/**
	 * Notifies all listeners that have registered interest for notification on
	 * this event type. The event instance is lazily created. The primary
	 * listeners will be views that need to be repainted because of changes in
	 * this model instance
	 * 
	 * @see EventListenerList
	 */
	public void fireStateChanged() {
		// Guaranteed to return a non-null array
		Object[] listeners = listenerList.getListenerList();
		// Process the listeners last to first, notifying
		// those that are interested in this event
		for (int i = listeners.length - 2; i >= 0; i -= 2) {
			if (listeners[i] == ChangeListener.class) {
				// Lazily create the event:
				if (changeEvent == null) {
					changeEvent = new ChangeEvent(eventSource);
				}
				((ChangeListener) listeners[i + 1]).stateChanged(changeEvent);
			}
		}
	}

}
